package kr.ac.hanyang.cs.park;

import java.util.ArrayList;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;


public class CastSpell {
	Activity activity								= null;
	Context context									= null;


	public CastSpell(Context context) {
		super();
		this.activity								= (Activity) context;
		this.context								= context;
	}


	public void castSkill(TaskType TaskType, String Option) {
		switch (TaskType) {
			case LAUNCH_APP:
				launchApp(Option);
				break;
			
			case CALL_ADDR:
				callAddr(Option);
				break;

			case SEND_SMS:
				sendSMS(Option);
				break;
				
			/*
			case FLASH_ON:
				setFlash(Stat.ON);
				break;

			case FLASH_OFF:
				setFlash(Stat.OFF);
				break;
			*/

			case WIFI_ON:
				setWifi(Stat.ON);
				break;

			case WIFI_OFF:
				setWifi(Stat.OFF);
				break;
				
			case BT_ON:
				setBT(Stat.ON);
				break;

			case BT_OFF:
				setBT(Stat.OFF);
				break;

			default:
	    		Log.e("ERROR", "NOTHING IMPLEMENTED YET @ "+ this.getClass().getSimpleName());
				break;
		}
	}


	static public String getTaskSuffix(TaskType taskType) {
		String taskSuffix							= "";

		switch(taskType) {
			case LAUNCH_APP:
				taskSuffix							= " 실행하기";
				break;
			
			case CALL_ADDR:
				taskSuffix							= "에게 전화걸기";
				break;
				
			case SEND_SMS:
				taskSuffix							= "에게 문자보내기";
				break;

			default:
				taskSuffix							= "";
		}
		
		return taskSuffix;
	}
	

	static public ArrayList<TaskInfo> getNewTaskList(Context context) {
		ArrayList<TaskInfo> allNewTask				= new ArrayList<TaskInfo>();
		TaskType[] TaskList							= TaskType.values();

		for (int i = 1;i < TaskList.length;i++) {
			TaskInfo newTask						= new TaskInfo();
			Drawable icon							= null;

			switch(TaskList[i]) {
				case LAUNCH_APP:
					icon							= context.getResources().getDrawable(android.R.drawable.ic_menu_more);
					newTask.setIcon(icon);
					newTask.setTaskName("외부 앱 실행");
					newTask.setDescription("단말기에 설치된 다른 앱을 실행합니다.");
					newTask.setTaskType(TaskList[i]);
					break;
				
				case CALL_ADDR:
					icon							= context.getResources().getDrawable(android.R.drawable.sym_action_call);
					newTask.setIcon(icon);
					newTask.setTaskName("전화걸기");
					newTask.setDescription("지정한 수신자에게 전화를 겁니다.");
					newTask.setTaskType(TaskList[i]);
					break;

				case SEND_SMS:
					icon							= context.getResources().getDrawable(android.R.drawable.sym_action_chat);
					newTask.setIcon(icon);
					newTask.setTaskName("문자보내기");
					newTask.setDescription("지정한 수신자에게 문자를 보냅니다.");
					newTask.setTaskType(TaskList[i]);
					break;

				/*
				case FLASH_ON:
					icon							= context.getResources().getDrawable(android.R.drawable.presence_online);
					newTask.setIcon(icon);
					newTask.setTaskName("플래시 켜기");
					newTask.setDescription("카메라 플래시를 켜서 손전등처럼 사용합니다.");
					newTask.setTaskType(TaskList[i]);
					break;
				
				case FLASH_OFF:
					icon							= context.getResources().getDrawable(android.R.drawable.presence_invisible);
					newTask.setIcon(icon);
					newTask.setTaskName("플래시 끄기");
					newTask.setDescription("카메라 플래시가 켜져있을 경우 끕니다.");
					newTask.setTaskType(TaskList[i]);
					break;
				*/
				
				case WIFI_ON:
					icon							= context.getResources().getDrawable(R.drawable.wifi_on);
					newTask.setIcon(icon);
					newTask.setTaskName("무선랜(WiFi) 켜기");
					newTask.setDescription("무선랜이 꺼져있을 경우 켭니다.");
					newTask.setTaskType(TaskList[i]);
					break;
				
				case WIFI_OFF:
					icon							= context.getResources().getDrawable(R.drawable.wifi_off);
					newTask.setIcon(icon);
					newTask.setTaskName("무선랜(WiFi) 끄기");
					newTask.setDescription("무선랜이 켜져있을 경우 끕니다.");
					newTask.setTaskType(TaskList[i]);
					break;

				case BT_ON:
					icon							= context.getResources().getDrawable(android.R.drawable.stat_sys_data_bluetooth);
					newTask.setIcon(icon);
					newTask.setTaskName("블루투스(Bluetooth) 켜기");
					newTask.setDescription("블루투스가 꺼져있을 경우 켭니다.");
					newTask.setTaskType(TaskList[i]);
					break;

				case BT_OFF:
					icon							= context.getResources().getDrawable(android.R.drawable.stat_sys_data_bluetooth);
					newTask.setIcon(icon);
					newTask.setTaskName("블루투스(Bluetooth) 끄기");
					newTask.setDescription("블루투스가 켜져있을 경우 끕니다.");
					newTask.setTaskType(TaskList[i]);
					break;

				default:
					icon							= context.getResources().getDrawable(android.R.drawable.ic_input_add);
					newTask.setIcon(icon);
					newTask.setTaskType(TaskList[i]);
					newTask.setIcon(null);
			}

			allNewTask.add(newTask);
		}
		
		return allNewTask;
	}
	
	
	static public TaskType getTaskType(String taskType) {
		TaskType[] allTaskType						= TaskType.values();
		TaskType selectedTaskType					= null;

		for (int i = 0;i < allTaskType.length;i++)
			if (taskType.equals(allTaskType[i].toString()))
				selectedTaskType					= allTaskType[i];
		
		return selectedTaskType;
	}

	
	
    // launch application using given package name (String pkgName)
	public void launchApp(String pkgName) {
		// TODO: should be check whether package exists or not

		Intent intent								= activity.getPackageManager().getLaunchIntentForPackage(pkgName);
		this.activity.startActivity(intent);
	}
	
	// call given phone number
	public void callAddr(String phoneNo) {
		// need permission
		// <uses-permission android:name="android.permission.CALL_PHONE" />
		// <uses-permission android:name="android.permission.CALL_PRIVILEGED" />

		Intent intent								= new Intent (Intent.ACTION_CALL,Uri.parse("tel:"+ phoneNo));
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		this.activity.startActivity(intent);
	}
	
	// send sms to given phone number
	public void sendSMS(String phoneNo) {
		ContentValues msgToSend						= new ContentValues();
		msgToSend.put("address", phoneNo);
		msgToSend.put("body", " ");

	    ContentResolver contentResolver				= context.getContentResolver();
	    contentResolver.insert(Uri.parse("content://sms/sent"), msgToSend);
	}
	
	// turn on/off flashlight
	public void setFlash(Stat toBeStat) {
		// need permission
		// <permission android:name="android.permission.FLASHLIGHT"
        // 	android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
        // 	android:protectionLevel="normal" />

		// TODO: should be check whether flashlight exists or not
		// check using - context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);


		// DOES NOT WORK -_-
		
		try {
			Camera camera							= Camera.open();     
			Parameters params						= camera.getParameters();

			switch (toBeStat) {
				case ON:
					//params.setFlashMode(Parameters.FLASH_MODE_ON);
					params.setFlashMode(Parameters.FLASH_MODE_TORCH);
					camera.setParameters(params);
					camera.startPreview();
					break;
	
				case OFF:
					camera.unlock();
					camera.stopPreview();
					camera.release();
					break;
			}
		}
		catch (Exception e) {
			Log.e("ERROR", "CAMERA ERROR @ "+ this.getClass().getSimpleName() + " || " + e.toString());
		}

	}


	// turn off screen
	public void screenOff() {
		// need permission (?)
		// <uses-permission android:name="android.permission.WAKE_LOCK" />
		
		// make screen brightness to zero
		Window myWindow								= activity.getWindow();
		WindowManager.LayoutParams lp				= myWindow.getAttributes();
		lp.screenBrightness							= 0;
		myWindow.setAttributes(lp);
	}
 

	// turn on/off wifi
	public void setWifi(Stat toBeStat) {
		// need permission
		// <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

		WifiManager wManager						= (WifiManager)activity.getSystemService(Context.WIFI_SERVICE);

		switch(toBeStat) {
			case ON:
				// turn on wifi
				wManager.setWifiEnabled(true);
				break;
			case OFF:
				// turn off wifi
				wManager.setWifiEnabled(false);
				break;
		}
	}


	// turn on/off bluetooth
	public void setBT(Stat toBeStat) {
		// need permission
		// <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
		// <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>

		BluetoothAdapter mBTAdapter				= BluetoothAdapter.getDefaultAdapter();

		if (mBTAdapter != null) {    
			switch(toBeStat) {
				case ON:
					// turn on BT
					mBTAdapter.enable();
					break;
				case OFF:
					// turn off BT
					mBTAdapter.disable();
			}
		}

	}
}
